package com.yhcnet.blog.service;

import com.yhcnet.blog.dao.DraftMapper;
import com.yhcnet.blog.pojo.Draft;
import com.yhcnet.blog.reponse.DraftBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DraftService {
    @Autowired
    DraftMapper draftMapper;

    @Autowired
    UserService userService;

    /**
     * 先判断是否存在草稿，如果存在覆盖，不存在新建一个
     * @param draftBody
     * @return
     */
    public boolean create(DraftBody draftBody){
        if(draftBody == null) return false;
        boolean judge = userService.judge(draftBody.getUuid(), draftBody.getEmail());
        if(!judge) return false;
        Draft d = getD(draftBody.getEmail());
        Draft draft = new Draft(0, draftBody.getTitle(), draftBody.getContent(), null, draftBody.getEmail());
        if(d == null){
            if(draftMapper.create(draft) > 0) return true;
        }
        if(draftMapper.update(draft) > 0 )return true;
        return false;
    }

    public boolean delete(String email){
        return draftMapper.delete(email) > 0 ;
    }

    public Draft getD(String email){
        return draftMapper.getD(email);
    }

}
